package com.dzl.enterprise;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.config.exception.CommonException;
import com.dzl.enterprise.pojo.query.WelfareEnterpriseApiGoodsQuery;
import com.dzl.enterprise.pojo.vo.WelfareEnterpriseApiGoodsVo;
import com.dzl.enterprise.service.WelfareEnterpriseApiGoodsService;
import com.dzl.goods.pojo.query.WelfareGoodsAttrQuery;
import com.dzl.goods.pojo.query.WelfareGoodsManageQuery;
import com.dzl.goods.pojo.vo.WelfareSkuGoodsVo;
import com.dzl.goods.pojo.vo.WelfareGoodsManageVo;
import com.dzl.goods.service.WelfareGoodsService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * api商品管理(welfare_enterprise_api_goods)表控制层
 * @author liuguanjun
 * @since 2021-11-23 17:27:41
 */
@RestController
@RequestMapping("welfareEnterpriseGoods")
public class WelfareEnterpriseApiGoodsController extends BaseController {


    @Autowired
    private WelfareEnterpriseApiGoodsService service;
    @Autowired
    private WelfareGoodsService welfareGoodsService;

    /**
     * 查询企业客户api商品列表
     * @param query 查询条件
     * @author liuguanjun
     * @date 2021-11-23 17:32:00
     */
    @GetMapping("getWelfareEnterpriseGoodsListPage")
    public PageUtils<WelfareEnterpriseApiGoodsVo> getWelfareEnterpriseGoodsListPage(WelfareEnterpriseApiGoodsQuery query) {
        if(EmptyUtils.isEmpty(query.getEnterpriseId())){
            throw new CommonException("企业id必传！");
        }
        PageInfo<WelfareEnterpriseApiGoodsVo> pageInfo = service.getWelfareEnterpriseGoodsListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 企业客户选择api商品，弹窗数据列表，以sku维度查
     * @param query 查询条件
     * @author liuguanjun
     * @date 2022-02-14 17:37:00
     */
    @GetMapping("getWelfareGoodsAttrListPage")
    public PageUtils<WelfareSkuGoodsVo> getWelfareGoodsAttrListPage(WelfareGoodsAttrQuery query) {
        if(query.getEnterpriseId() == null){
            throw new CommonException("企业id必传！");
        }
        query.setQuerySource(1);
        PageInfo<WelfareSkuGoodsVo> pageInfo = welfareGoodsService.getWelfareGoodsAttrListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 企业客户选择api商品，弹窗数据列表，以商品维度查 （已作废）
     * @param query 查询条件
     * @author liuguanjun
     * @date 2021-11-23 17:32:00
     */
   /* @GetMapping("getWelfareGoodsListPage")
    public PageUtils<WelfareGoodsManageVo> getWelfareGoodsListPage(WelfareGoodsManageQuery query) {
        PageInfo<WelfareGoodsManageVo> pageInfo = service.getWelfareGoodsListPage(query);
        return new PageUtils<>(pageInfo);
    }*/

    /**
     * 添加实体商品，保存
     * @param param 商品skuid等传参
     * @author liuguanjun
     * @date 2021-11-23 18:01:00
     */
    @PostMapping("saveWelfareEnterpriseGoods")
    public JsonResult<Integer> saveWelfareEnterpriseGoods(@RequestBody JSONObject param) {
        String skuIds = param.getString("skuIds"); //商品skuid
        Integer enterpriseId = param.getInteger("enterpriseId");//供应商id
        if(EmptyUtils.isEmpty(skuIds)) {
            return JsonResult.paramErrWithMsg("商品skuid不能传空");
        }
        if(EmptyUtils.isEmpty(enterpriseId)) {
            return JsonResult.paramErrWithMsg("企业id不能传空");
        }
        return JsonResult.success(service.saveWelfareEnterpriseGoods(skuIds,enterpriseId));
    }

    /**
     * 批量删除供应商商品
     * @param ids 商品id
     * @author liuguanjun
     * @date 2021-11-23 17:30:00
     */
    @GetMapping("deleteWelfareEnterpriseGoods")
    public JsonResult<Integer> deleteWelfareEnterpriseGoods(@RequestParam("ids") String ids) {
        return JsonResult.success(service.deleteWelfareEnterpriseGoods(ids));
    }

    /**
     * 修改供应商品价格
     * @param param 商品id等传参
     * @author liuguanjun
     * @date 2021-11-23 17:30:00
     */
    @PostMapping("updateWelfareEnterpriseGoodsSupplyPriceById")
    public JsonResult<Integer> updateWelfareEnterpriseGoodsSupplyPriceById(@RequestBody JSONObject param) {
        Integer id = param.getInteger("id");//商品id
        BigDecimal supplyPrice = param.getBigDecimal("supplyPrice");//供应商价格
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("商品id不能传空");
        }
        if(EmptyUtils.isEmpty(supplyPrice)) {
            return JsonResult.paramErrWithMsg("供应商价格不能传空");
        }
        return JsonResult.success(service.updateWelfareEnterpriseGoodsSupplyPriceById(id,supplyPrice));
    }

    /**
     * 批量修改供应商品价格
     * @param param 商品id等传参
     * @author liuguanjun
     * @date 2021-11-23 17:30:00
     */
    @PostMapping("batchUpdateWelfareEnterpriseGoodsSupplyPrice")
    public JsonResult<Integer> batchUpdateWelfareEnterpriseGoodsSupplyPrice(@RequestBody JSONObject param) {
        String ids = param.getString("ids"); //商品id
        Double floatingRatio = param.getDouble("floatingRatio");//是否首页显示：0否 1是
        if(EmptyUtils.isEmpty(ids)) {
            return JsonResult.paramErrWithMsg("商品id不能传空");
        }
        if(EmptyUtils.isEmpty(floatingRatio)) {
            return JsonResult.paramErrWithMsg("上浮比例不能传空");
        }
        return JsonResult.success(service.batchUpdateWelfareEnterpriseGoodsSupplyPrice(ids,floatingRatio));
    }

    /**
     * 根据商品id查询sku数据列表
     * @param goodsId 商品id
     * @author liuguanjun
     * @date 2021-11-23 17:32:00
     */
    @GetMapping("getGoodsAttrsByGoodsIdAndEnterpriseId")
    public JsonResult<List<WelfareSkuGoodsVo>> getGoodsAttrsByGoodsIdAndEnterpriseId(@RequestParam("goodsId") Integer goodsId, @RequestParam("enterpriseId") Integer enterpriseId) {
        List<WelfareSkuGoodsVo> list = service.getGoodsAttrsByGoodsIdAndEnterpriseId(goodsId,enterpriseId);
        return JsonResult.success(list);
    }


}
